
import { ajax } from 'nc-lightapp-front';
import { PAGECODE, AREA, UISTATUS, URL, FIELDS, CARDTABLEAREAIDS, DATASOURCECACHE } from '../../constance';
import { showSuccessInfo } from '../../tool/messageUtil';
import { setCardPage } from '../utils/cardPageUtil';
import { addCacheData, updateCacheData } from '../../tool/cacheDataManager';
import { getLangByResId } from '../../tool/multiLangUtil';
import { buttonControl } from '../viewControl/buttonControl';

export default function(props) {
        // 过滤表中空行
        filterEmptyRows();
        // 表头必输项校验
        let flag = props.form.isCheckNow(AREA.cardFormId);
        if (!flag) return;
        // 表体必输项校验
        let bodyFlag = props.cardTable.checkTableRequired(CARDTABLEAREAIDS);
        if (!bodyFlag) return;

        let data = props.createExtCardData(PAGECODE.cardPagecode, AREA.cardFormId, CARDTABLEAREAIDS);

        props.validateToSave(data, () => {
                ajax({
                        url: URL.save,
                        data: data,
                        success: (res) => {
                                if (res.formulamsg && res.formulamsg instanceof Array && res.formulamsg.length > 0) {
                                        props.dealFormulamsg(
                                                res.formulamsg //参数一：返回的公式对象
                                        );
                                }
                                if (res.success && res.data) {
                                        let pk = res.data.head[AREA.cardFormId].rows[0].values[FIELDS.pk_head_fields].value;
                                        let status = props.getUrlParam('status');

                                        // 差异更新，补全数据
                                        CARDTABLEAREAIDS.map((moduleId) => {
                                                if (res.data.bodys && res.data.bodys[moduleId]) {
                                                        let fullTableData = props.cardTable.updateDataByRowId(
                                                                moduleId,
                                                                res.data.bodys[moduleId],
                                                                true
                                                        );
                                                        res.data.bodys[moduleId] = fullTableData;
                                                }
                                        });

                                        if (status == UISTATUS.add || status == UISTATUS.copy) {
                                                addCacheData(
                                                        props,
                                                        FIELDS.pk_head_fields,
                                                        pk,
                                                        res.data,
                                                        AREA.cardFormId,
                                                        DATASOURCECACHE.dataSourceListCacheKey
                                                );
                                                // 更新翻页组件当前pk值
                                                props.cardPagination.setCardPaginationId({ id: pk, status: 1 });
                                        } else {
                                                updateCacheData(
                                                        props,
                                                        FIELDS.pk_head_fields,
                                                        pk,
                                                        res.data,
                                                        AREA.cardFormId,
                                                        DATASOURCECACHE.dataSourceListCacheKey
                                                );
                                        }
                                        props.setUrlParam({ status: UISTATUS.browse });
                                        setCardPage.call(this, props, res.data);
                                        showSuccessInfo(getLangByResId(this, '4001PUBMESSAGE-0000002')); /* 国际化处理： 保存成功*/
                                }
                                buttonControl.call(this, props);
                        }
                });
        });
}

function filterEmptyRows() {
        // props.cardTable.filterEmptyRows(AREA.cardBody_1, [], 'except');
}
